package demo.axitdn.model;

import android.graphics.Canvas;
import android.view.MotionEvent;
import demo.axitdn.event.MultiEventListener;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Force;
import demo.axitdn.physic.RK4V;
import demo.axitdn.physic.State;

public abstract class Model implements TouchListener {
	private TouchListener touchListener;	
	protected State current;
	protected State previous;
	protected State state;
	protected RK4V rk4;
	protected float t=0;
	protected float dt = 0.01f;
	public Model() {
		state=new State();
		current=state;
		previous=current;
		addTouchListener(this);
	}
	
	public State getState() {
		return state;
	}
	
	public void setForce(Force f) {
		rk4=new RK4V(f);
	}
	
	public abstract void onDraw(Canvas c);
	public abstract void onUpdate(double t);
	
	
	public void integrate(double t,float dt) {
		 previous = current;
         rk4.integrate(current, t, dt);
	}
	
	public void interpolate(double alpha) {
		state=rk4.interpolate(previous, current, (float)alpha);
		current=state;
	}
	
	public void addTouchListener(TouchListener l) {
		touchListener = MultiEventListener.add(touchListener, l);
    }

    public void removeTouchListener(TouchListener l) {
    	touchListener = MultiEventListener.remove(touchListener, l);
    }
    
    public void fireTouchEvent(MotionEvent event)
    {
    	int action = event.getAction();
		switch (action){
			case MotionEvent.ACTION_DOWN:
				touchListener.onPressed((int)event.getX(), (int)event.getY());
				break;
			case MotionEvent.ACTION_UP:
				touchListener.onReleased((int)event.getX(), (int)event.getY());
				break;
			case MotionEvent.ACTION_MOVE:
				touchListener.onDragged((int)event.getX(), (int)event.getY());
				break;
		}//end switch
    }

	public boolean onPressed(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onDragged(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean onReleased(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}
}
